#include <pthread.h>
#include <iostream>

#include "SendManager.hpp"
#include "ClientThread.hpp"
#include "Listener.hpp"
#include "Xml.hpp"


#define PORT 5500
#define MAX_CLIENT_PER_THREAD 1024


using namespace std;

void* threadFunction(void* arg);

int main(int argc,char**argv)
{
/*
	Xml* test=new Xml();
	test->addNode((char*)"1");
	test->addNode((char*)"2");
	test->addNode((char*)"3");
	
	(*test)["1"].addChild("2");
	(*test)["1"]["2"].addChild("3","poulet");


	cout << "XML : \n" << test->writeXml() << endl;






	return 0;
*/
	/* pthread_t */
	pthread_t* listenThread=new pthread_t;
	pthread_t* clientThread=new pthread_t;
	pthread_t* requestManagerThread=new pthread_t;
	pthread_t* sendManagerThread=new pthread_t;

	/* Create server non-thread objects */
	ClientManager* clientManager=new ClientManager;

	/* Create server thread objects */
	RequestManager* r=new RequestManager(clientManager);
	ClientThread* c=new ClientThread(MAX_CLIENT_PER_THREAD,r,clientManager);
	Listener* l=new Listener(PORT,c,1);
	SendManager* s=new SendManager(clientManager);

	/* launch threads */
	pthread_create(listenThread,NULL,threadFunction,(void*)l);
	pthread_create(clientThread,NULL,threadFunction,(void*)c);
	pthread_create(requestManagerThread,NULL,threadFunction,(void*)r);
	pthread_create(sendManagerThread,NULL,threadFunction,(void*)s);

	/* join threads */
	pthread_join(*clientThread,NULL);
	pthread_join(*listenThread,NULL);
	pthread_join(*requestManagerThread,NULL);
	pthread_join(*sendManagerThread,NULL);

	/* end */
	return 0;
}

void* threadFunction(void* arg)
{
	Runnable* r=(Runnable*)arg;
	r->run();
	return NULL;
}
